package recruit_service

import (
	"encoding/json"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"

	"github.com/gin-gonic/gin"
)

type RecruitService struct{}

// 获取行业列表
func (rs *RecruitService) GetIndustry(ctx *gin.Context) []string {
	var out = []string{"全部"}
	d := variable.DB.MySQL.Model(&tables.SchoolPositionCompany{}).Distinct("industry").Find(&out)
	if d.Error != nil {
		return out
	}
	var rest = []string{"全部"}
	return append(rest, out...)
}

// 获取地区列表
func (rs *RecruitService) GetAddress(ctx *gin.Context) []string {
	var out = []string{"全部"}
	d := variable.DB.MySQL.Model(&tables.City{}).Distinct("name").Order("order_id desc").Find(&out)
	if d.Error != nil {
		return out
	}
	var rest = []string{"全部"}
	return append(rest, out...)
}

// 获取校园招聘列表
func (rs *RecruitService) GetRecruit(ctx *gin.Context, mr model.RecruitReq) ([]model.RecruitResp, int64, error) {
	page := mr.Page
	pageSize := mr.PageSize
	industry := mr.Industry
	position := mr.Position
	var spc []tables.SchoolPositionCompany
	var out []model.RecruitResp
	var count int64 = 0

	if industry == "全部" && position == "全部" {
		d := variable.DB.MySQL.Model(&tables.SchoolPositionCompany{}).Where("is_release = 0").Order("created_at desc").Offset(int((page - 1) * pageSize)).Limit(int(pageSize)).Find(&spc).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, count, d.Error
		}
	} else if industry == "全部" && position != "全部" {
		var citys tables.City
		d := variable.DB.MySQL.Model(&tables.City{}).Where("name = ?", position).Find(&citys)
		if d.Error != nil {
			return out, count, d.Error
		}
		cids := citys.SchoolPositionCompanyIds
		var cid = make([]int, 0)
		json.Unmarshal([]byte(cids), &cid)

		d = variable.DB.MySQL.Model(&tables.SchoolPositionCompany{}).Where("id in (?) and is_release = 0", cid).Order("created_at desc").Offset(int((page - 1) * pageSize)).Limit(int(pageSize)).Find(&spc).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, count, d.Error
		}
	} else if industry != "全部" && position == "全部" {
		d := variable.DB.MySQL.Model(&tables.SchoolPositionCompany{}).Where("is_release = 0 and industry = ?", industry).Order("created_at desc").Offset(int((page - 1) * pageSize)).Limit(int(pageSize)).Find(&spc).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, count, d.Error
		}
	} else if industry != "全部" && position != "全部" {
		var citys tables.City
		d := variable.DB.MySQL.Model(&tables.City{}).Where("name = ?", position).Find(&citys)
		if d.Error != nil {
			return out, count, d.Error
		}
		cids := citys.SchoolPositionCompanyIds
		var cid = make([]int, 0)
		json.Unmarshal([]byte(cids), &cid)

		d = variable.DB.MySQL.Model(&tables.SchoolPositionCompany{}).Where("id in (?) and is_release = 0 and industry = ?", cid, industry).Order("created_at desc").Offset(int((page - 1) * pageSize)).Limit(int(pageSize)).Find(&spc).Offset(-1).Limit(-1).Count(&count)
		if d.Error != nil {
			return out, count, d.Error
		}
	}

	for _, v := range spc {
		var rr model.RecruitResp
		var c = make([]string, 0)
		json.Unmarshal([]byte(v.CityNameList), &c)
		rr.CityNameList = c

		c = make([]string, 0)
		json.Unmarshal([]byte(v.PositionCategoryList), &c)
		rr.PositionCategoryList = c

		c = make([]string, 0)
		json.Unmarshal([]byte(v.Detail), &c)
		rr.Detail = c

		rr.CompanyName = v.CompanyName
		rr.CompanyLogo = v.CompanyLogo
		rr.CompanyAddress = v.CompanyAddress
		rr.Industry = v.Industry
		rr.YearsOfExperience = v.YearsOfExperience
		rr.DateOfGraduation = v.DateOfGraduation
		rr.PositionType = v.PositionType
		rr.DeliverAddress = v.DeliverAddress
		rr.PositionDeadline = v.PositionDeadline

		out = append(out, rr)
	}

	return out, count, nil
}
